program tangqiao;

  uses crt;

  const ia=2;ib=60;
        ja=3;jb=24;
  const bug_long=50;
        up=1;
        left=2;
        down=3;
        right=4;
        yuan='yellow';
        order:set of char=['i','j','k','l'];
        speedchar:set of char=['a','s'];
  var bug:array[1..50] of 1..4;
      x,y,foodx,foody:integer;
      run,temp:char;
      long,v:integer;
      die:boolean;
      number_eat:integer;
      total:longint;

procedure name;
  begin
    textcolor(3);
    gotoxy(25,1);
    write('Eating Snake');
    gotoxy(37,2);
    write(' Tang Qiao  2001.10.4.');
  end;


procedure draw;
  var a,b,i:integer;
  begin
    a:=x;
    b:=y;
    gotoxy(a,b);
    write('*');
    for i:=1 to long-1 do
      begin
        case bug[i] of
          up:dec(b);
          left:dec(a);
          down:inc(b);
          right:inc(a);
        end;
        gotoxy(a,b);
        write('*');
      end;
  end;

procedure drawrun;
  var a,b,i:integer;
  begin
    for i:=long downto 2 do bug[i]:=bug[i-1];
     case run of
        'i':bug[1]:=down;  { up }
        'j':bug[1]:=right;  {left}
        'k':bug[1]:=up;  {down}
        'l':bug[1]:=left;  {right}
    end;
    a:=x;
    b:=y;
    gotoxy(a,b);
    write('*');
    for i:=1 to long do
      begin
        case bug[i] of
          up:dec(b);
          left:dec(a);
          down:inc(b);
          right:inc(a);
        end;
        if i<>long then
          begin
            gotoxy(a,b);
            write('*');
          end
                   else
          begin
            gotoxy(a,b);
            write(' ');
          end;
      end;
  end;

procedure draw_eat;   {y=6}
  begin
    inc(number_eat);
    gotoxy(ib+2+13,6);
    write(number_eat);
  end;
procedure draw_long;
  begin
    inc(long);
    gotoxy(ib+2+13,8);
    write(long);
  end;
procedure draw_total;
  begin
    textcolor(yellow);
    inc(total);
    gotoxy(ib+2+13,10);
    write(total);
    textcolor(white);
  end;
procedure draw_speed(s:char);
  begin
    case s of
      'a':begin
            if v>50 then dec(v,50);
          end;
      's':begin
            if v<1000 then inc(v,50);
          end;
    end;
    textcolor(yellow);
    gotoxy(ib+2+13,4);
    write(1000-v);
  end;

procedure init_wall;     {2,3 to 55,3} {2,23 to 55,23}

  var i,j:integer;
  begin
    j:=ja;
    for i:=ia to ib do
      begin
        gotoxy(i,j);
        write('#');
      end;
    j:=jb;
    for i:=ia to ib do
      begin
        gotoxy(i,j);
        write('#');
      end;
    i:=ia;
    for j:=ja to jb do
      begin
        gotoxy(i,j);
        write('#');
      end;
    i:=ib;
    for j:=ja to jb do
      begin
        gotoxy(i,j);
        write('#');
      end;
  end;


procedure init(a:integer);
  var i:integer;
  begin
    number_eat:=0;
    total:=0;
    v:=300;
    long:=a;
    x:=20;
    y:=10;
    fillchar(bug,sizeof(bug),0);
    for i:=1 to a do   { make the bug }
      bug[i]:=2;
    init_wall;  {make the wall}

    gotoxy(ib+2,4);
    write('Your spead:  ');
    gotoxy(ib+2,6);
    write('Eat number:  ');
    gotoxy(ib+2,8);
    write('Your longth: ');
    gotoxy(ib+2,10);
    write('Total score: ');

    gotoxy(ib+2,12);
    write('i -  up  k - down');
    gotoxy(ib+2,14);
    write('j - left l - right');
    gotoxy(ib+2,16);
    write('a --- faster');
    gotoxy(ib+2,18);
    write('s --- slower');


    textcolor(yellow);
    gotoxy(ib+2+13,4);write(1000-v);
    gotoxy(ib+2+13,6);write(number_eat);
    gotoxy(ib+2+13,8);write(long);

    textcolor(red);
    gotoxy(ib+2,12);write('i');
    gotoxy(ib+11,12);write('k');
    gotoxy(ib+2,14);write('j');
    gotoxy(ib+11,14);write('l');
    gotoxy(ib+2,16);write('a');
    gotoxy(ib+2,18);write('s');
  end;

function hit_wall:boolean;
  var i,j:integer;
  begin
    if (x=ia)or(x=ib)or(y=ja)or(y=jb) then hit_wall:=true
                                      else hit_wall:=false;
  end;

function if_die(ya,yb:integer):boolean;
  var a,b,i:integer;
  begin
    a:=x;b:=y;
    for i:=1 to long-1 do
      begin
            case bug[i] of
              up:dec(b);
              left:dec(a);
              down:inc(b);
              right:inc(a);
            end;
            if (a=ya)and(b=yb) then
              begin
                if_die:=true;
                exit;
              end;
         if hit_wall then
           begin
             if_die:=true;
             exit;
           end;
      end;
    if_die:=false;
  end;

 {* make the food site *}
procedure food(var foodx,foody:integer);
  var k:boolean;
      i,a,b:integer;
  begin
    randomize;
    repeat
       foodx:=random(ib-ia-2)+ia+1;
       foody:=random(jb-ja-2)+ja+1;
       k:=true;
       a:=x;b:=y;
       for i:=1 to long-1 do
         begin
            if (a=foodx)and(b=foody) then
              begin
                k:=false;
                break;
              end;
            case bug[i] of
              up:dec(b);
              left:dec(a);
              down:inc(b);
              right:inc(a);
            end;
         end;

    until k;
    gotoxy(foodx,foody);
    textcolor(green);
    write('@');
    textcolor(white);
  end;

begin      {** main program **}
    clrscr;
    init(5);
    draw;
    name;
    run:='l';
    food(foodx,foody);
    food(foodx,foody);
    repeat
      delay(v);
      if keypressed then
        begin
          temp:=readkey;
          if temp=#27 then halt;
          if temp in speedchar then draw_speed(temp);
          if temp in order then run:=temp;
          case run of
             'i':dec(y);  { up }
             'j':dec(x);  {left}
             'k':inc(y);  {down}
             'l':inc(x);  {right}
          end;
        end
                    else
        begin
          case run of
             'i':dec(y);  { up }
             'j':dec(x);  {left}
             'k':inc(y);  {down}
             'l':inc(x);  {right}
          end;
        end;
        if (x=foodx)and(y=foody) then
          begin
           food(foodx,foody);
           textcolor(yellow);
           draw_eat;
           draw_long;
           textcolor(white);
          end;
        textcolor(3);
        drawrun;
      die:=if_die(x,y);
      draw_total;
    until die;
    if die then
      begin
        gotoxy(x,y);
        textcolor(red);
        write('*');
        textcolor(3);
        gotoxy(ib+2,20);
        write('Sorry,you are dead.');
        gotoxy(ib+2,22);
        textcolor(red);
        write('Thank for using!');
        gotoxy(17,jb);
        textcolor(3);
        write('E-mail:tang19840903@sina.com');
      end;
end.


